﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Data.Xml.Dom;
using Windows.Storage;

namespace Beyondsoft.Tuanju.DataManager
{
    public class Deal
    {
        public string Website
        {
            get;
            set;
        }

        public string UriLink
        {
            set;
            get;
        }

        public string Title
        {
            get;
            set;
        }

        public string Gid
        {
            get;
            set;
        }

        public Category Category
        {
            get;
            set;
        }

        public double Value
        {
            get;
            set;
        }

        public double Discount
        {
            get;
            set;
        }

        public double Price
        {
            get;
            set;
        }

        public int Bought
        {
            get;
            set;
        }

        public DateTime StartTime
        {
            get;
            set;
        }

        public DateTime EndTime
        {
            get;
            set;
        }

        public int ExpireTime
        {
            get;
            set;
        }

        public string Image
        {
            get;
            set;
        }

        public string Description
        {
            set;
            get;
        }

        public string Details
        {
            set;
            get;
        }

        public string ShortTitle
        {
            set;
            get;
        }
    }

    public enum Category
    {
        全部,
        其他,
        餐饮美食,
        网络购物,
        休闲娱乐,
        酒店旅游,
        生活服务
    }

    public class DealGroup
    {
        private static DealGroup dealGroup = null;

        private static Dictionary<Category, DealGroup> groups = new Dictionary<Category, DealGroup>();

        public Category Category
        {
            get;
            set;
        }

        public IEnumerable<Deal> Deals
        {
            set;
            get;
        }

        public Dictionary<Category, DealGroup> Groups
        {
            get
            {
                return groups;
            }
        }

        public string Title
        {
            get
            {
                return this.Category.ToString();
            }
        }

        public string Description
        {
            get;
            set;
        }

        public string Image
        {
            get;
            set;
        }

        private DealGroup() { }

        private DealGroup(Category category, string discription, string image)
        {
            this.Category = category;
            this.Description = discription;
            this.Image = image;
        }

        public async static Task<DealGroup> GetDealGroupInstance() 
        {
           await FetchDealGroups();

            if (dealGroup == null) 
            {
                dealGroup = new DealGroup();
            }

            return dealGroup;
        }

        private async static Task<Dictionary<Category,DealGroup>> FetchDealGroups()
        {
            StorageFolder storageFolder = await Package.Current.InstalledLocation.GetFolderAsync("Beyondsoft.Tuanju.DataManager");
            StorageFolder dataFolder = await storageFolder.GetFolderAsync("Configuration");

            var groupsFile = await dataFolder.GetFileAsync("Groups.xml");
            XmlDocument xdoc = await XmlDocument.LoadFromFileAsync(groupsFile);
            var groupNodes = xdoc.SelectNodes("groups/group");

            groups.Clear();

            foreach (var node in groupNodes)
            {
                string title = node.SelectSingleNode("title").InnerText;
                string discription = node.SelectSingleNode("description").InnerText;
                string image = node.SelectSingleNode("image").InnerText;
                Category category = (Category)Enum.Parse(typeof(Category), title, true);
                DealGroup dealgroup = new DealGroup(category, discription, image);

                IEnumerable<Deal> deals = new List<Deal>();
                deals = await City.DefaultCity.GetDeals(category);

                dealgroup.Deals = deals;
                groups.Add(category, dealgroup);
            }

            return groups;
        }

    }
}
